package com.chen.music.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chen.music.model.Admin;
import com.chen.music.service.AdminService;
import com.chen.music.util.CommonResult;
import com.chen.music.util.MessageConst;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 管理员 前端控制器
 * </p>
 *
 * @author boat
 * @since 2022-01-13
 */
@RestController
@RequestMapping("/admin")
@Api(tags = "后台用户管理")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public CommonResult login(Admin admin) {
        LambdaQueryWrapper<Admin> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(!StringUtils.isEmpty(admin.getName()), Admin::getName, admin.getName());
        wrapper.eq(!StringUtils.isEmpty(admin.getPassword()), Admin::getPassword, DigestUtils.md5DigestAsHex(admin.getPassword().getBytes()));
        try {
            if (adminService.count(wrapper) > 0) {
                return new CommonResult(200, true, MessageConst.ADMIN_LOGIN_SUCCESS);
            }
            return new CommonResult(200, false, MessageConst.ADMIN_LOGIN_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(500, false, MessageConst.INTERFACE_EXCEPTION);
        }
    }

}

